package com.spring.demo.rabbit.listener;

import com.spring.demo.rabbit.ExchangeConst;
import com.spring.demo.rabbit.QueueConst;
import com.spring.demo.rabbit.RoutingKeyConst;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;

/***
 * @title TopicListener
 * @description： 根据Routing Key 和交换器 中banding 的队列 Routing Key 进行模糊匹配， ”#“匹配0个或多个， ”*“ 匹配一个
 * @author baiji
 * @version 1.0.0
 * @create 2023/9/4 9:41
 **/
@Component
public class TopicListener {

    @RabbitListener(bindings = @QueueBinding(value = @Queue(value = QueueConst.TOPIC_QUEUE_01),
            key = RoutingKeyConst.TOPIC_ROUTING_KEY_01,
            exchange = @Exchange(value = ExchangeConst.TOPIC_EXCHANGE, type = "topic")
        )
    )
    public void handler01(Message message) {
        String body = new String(message.getBody(), StandardCharsets.UTF_8);
        System.out.println("TOPIC_QUEUE_01 receive msg: " + body);
    }

    @RabbitListener(bindings = @QueueBinding(value = @Queue(value = QueueConst.TOPIC_QUEUE_02),
            key = RoutingKeyConst.TOPIC_ROUTING_KEY_02,
            exchange = @Exchange(value = ExchangeConst.TOPIC_EXCHANGE, type = "topic")
    )
    )
    public void handler02(Message message) {
        String body = new String(message.getBody(), StandardCharsets.UTF_8);
        System.out.println("TOPIC_QUEUE_02 receive msg: " + body);
    }

    @RabbitListener(bindings = @QueueBinding(value = @Queue(value = QueueConst.TOPIC_QUEUE_03),
            key = RoutingKeyConst.TOPIC_ROUTING_KEY_03,
            exchange = @Exchange(value = ExchangeConst.TOPIC_EXCHANGE, type = "topic")
    )
    )
    public void handler03(Message message) {
        String body = new String(message.getBody(), StandardCharsets.UTF_8);
        System.out.println("TOPIC_QUEUE_03 receive msg: " + body);
    }

}
